﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Resources;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using Next.CodeGen.Core.Utility;

namespace Next.CodeGen.Core.Generators
{
    public class Forms
    {
        public static void GenerateForms(List<string> Tables)
        {
            
            //ResXResourceWriter resx = new ResXResourceWriter(@Properties.Settings.Default.FormsPath + "\\res.resx");
            //resx.AddResource("name", "value");
            //resx.Close();
            int TablesFound = 0;
            try
            {
                Console.WriteLine("Forms generation started.");
                List<Table> ListTables = XMLSchemaUtility.XMLToList();
                foreach (var itemTable in ListTables)
                {
                    if (Tables == null || Tables.Contains(itemTable.Name))
                    {
                        TablesFound++;
                        foreach (var itemColumn in itemTable.Columns)
                        {
                            if (itemColumn.IsIdentity == null || itemColumn.IsIdentity != "True")
                            {
                                string XAMLContent = GenerateXAMLSingleTextTemplate(itemColumn.Name, "NameSpace");
                                string CSContent = GenerateCSSingleTextTemplate(itemColumn.Name, "NameSpace");
                                SaveForm(XAMLContent, CSContent, CreateTableFormFolder(itemTable.Name) + "\\" + itemColumn.Name + "UC");
                            }
                        }
                        foreach (var itemRelation in itemTable.Relations)
                        {
                            string XAMLContent = GenerateXAMLComboBoxTemplate(itemRelation.LocalColumn, "NameSpace");
                            string CSContent = GenerateCSSingleComboBoxTemplate(itemRelation.LocalColumn, "NameSpace");
                            SaveForm(XAMLContent, CSContent, CreateTableFormFolder(itemTable.Name) + "\\" + itemRelation.LocalColumn + "UC");
                        }
                    }
                }
                if (Tables != null && Tables.Count != TablesFound)
                {
                    Console.WriteLine("Forms generated with errors : some tables could not be found!");
                }
                else
                {
                    Console.WriteLine("Forms generated successfully!");
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Error : Schema File not found, please generate schema before generating forms.");
            }
        }

        private static string GenerateXAMLSingleTextTemplate(string ColumnName, string NameSpace)
        {
            string XAMLContent = "";
            using (StreamReader XAML = new StreamReader(@Properties.Settings.Default.CoreProjectPath + "\\ClassTemplates\\SingleTextFormTemplate.xaml.next"))
            {
                XAMLContent = XAML.ReadToEnd();
                XAMLContent = XAMLContent.Replace("{Name}", ColumnName)
                                         .Replace("{lblDefaultContent}", ColumnName)
                                         .Replace("{txtDefaultContent}", "")
                                         .Replace("{NameSpace}", NameSpace)
                                         .Replace("{ClassName}", ColumnName + "UC");
            }
            return XAMLContent;
        }

        private static string GenerateCSSingleTextTemplate(string ColumnName, string NameSpace)
        {
            string CSContent = "";
            using (StreamReader XAML = new StreamReader(@Properties.Settings.Default.CoreProjectPath + "\\ClassTemplates\\SingleTextFormTemplate.xaml.cs.next"))
            {
                CSContent = XAML.ReadToEnd();
                CSContent = CSContent.Replace("{Name}", ColumnName)
                                     .Replace("{lblDefaultContent}", ColumnName)
                                     .Replace("{txtDefaultContent}", NameSpace)
                                     .Replace("{NameSpace}", NameSpace)
                                     .Replace("{ClassName}", ColumnName + "UC");
            }
            return CSContent;
        }

        private static string GenerateXAMLComboBoxTemplate(string ColumnName, string NameSpace)
        {
            string XAMLContent = "";
            using (StreamReader XAML = new StreamReader(@Properties.Settings.Default.CoreProjectPath + "\\ClassTemplates\\ComboBoxFormTemplate.xaml.next"))
            {
                XAMLContent = XAML.ReadToEnd();
                XAMLContent = XAMLContent.Replace("{Name}", ColumnName)
                                         .Replace("{lblDefaultContent}", ColumnName)
                                         .Replace("{txtDefaultContent}", "")
                                         .Replace("{NameSpace}", NameSpace)
                                         .Replace("{ClassName}", ColumnName + "UC");
            }
            return XAMLContent;
        }

        private static string GenerateCSSingleComboBoxTemplate(string ColumnName, string NameSpace)
        {
            string CSContent = "";
            using (StreamReader XAML = new StreamReader(@Properties.Settings.Default.CoreProjectPath + "\\ClassTemplates\\ComboBoxFormTemplate.xaml.cs.next"))
            {
                CSContent = XAML.ReadToEnd();
                CSContent = CSContent.Replace("{Name}", ColumnName)
                                     .Replace("{lblDefaultContent}", ColumnName)
                                     .Replace("{txtDefaultContent}", NameSpace)
                                     .Replace("{NameSpace}", NameSpace)
                                     .Replace("{ClassName}", ColumnName + "UC");
            }
            return CSContent;
        }

        /// <summary>
        /// Save the content of the XAML and CS files in specified path.
        /// </summary>
        /// <param name="FormXAML">The content of the XAML file.</param>
        /// <param name="FormCS">The content of the CS file.</param>
        /// <param name="FormPath">The full file path without extension, 
        /// the method will append automatically .xaml to XAML file and .xaml.cs to CS file.
        /// </param>
        private static void SaveForm(string FormXAML, string FormCS, string FormPath)
        {
            using (StreamWriter File = new StreamWriter(FormPath + ".xaml"))
            {
                File.WriteLine(FormXAML);
            }
            using (StreamWriter File = new StreamWriter(FormPath + ".xaml.cs"))
            {
                File.WriteLine(FormCS);
            }
        }

        /// <summary>
        /// Creates and returns a separate folder for the specified TableName under FormsPath folder.
        /// </summary>
        /// <param name="TableName">Table name</param>
        /// <returns></returns>
        private static string CreateTableFormFolder(string TableName)
        {
            string FullTableFormFolder = Properties.Settings.Default.FormsPath + "\\" + TableName;
            Directory.CreateDirectory(FullTableFormFolder);
            return FullTableFormFolder;
        }
    }
}